/**
 * 我的作业、单据稽核节点变更第二层iframe的url时需要用到此工具行数
 * @param {*} data  requestApi.openBill接口返回的数据
 * @param {*} extQueryParams  扩展的查询参数
 * @param {*} iframeWrapId  是否是有翻页器触发的url变换。如果是需要加上时间戳让iframe刷新。
 */
function urlHandle(data, extQueryParams, iframeWrapId, iframeId) {
    let url = data.data.url
    let params = data.data.data

    let start = '';
    if (data.data.system == 'YBZ') {
        url += '&'
        /* if (isPaging) {
            url += `ssc_r_time=${new Date().getTime()}&`
        } */
    } else {
        if (url.indexOf('#') > -1) {
            // if (url.indexOf('#/') > -1) {
                if (url.indexOf('?') < 0) {
                    url += '?'
                }
            // }
        } else {
            url += '#/?'
        }
        /* if (isPaging) {
            let sscReloadTimeStamp = `ssc_r_time=${new Date().getTime()}`;

            let splitUrl = url.split('#');
            let searchPos = splitUrl[0].indexOf('?');
            if (searchPos > -1) {
                splitUrl[0] = splitUrl[0].split('?').join(`?${sscReloadTimeStamp}&`);
            } else {
                splitUrl[0] += `?${sscReloadTimeStamp}`
            }
            url = splitUrl.join('#');
        } */
        start = url.indexOf('/nccloud/resources') > -1 ? '' : '/nccloud/resources';
    }

    let approveUrl = start + url + extQueryParams

    for (let attr in params) {
        approveUrl += `&${attr}=${params[attr]}`
    }
    //平台getUrlParm不支持url中既有search参数又有hash参数场景，如url跨域，翻页仅hash参数变化，仅能用这种方式控制iframe reload
    if (iframeWrapId || iframeId) {
        let iframeWrapObj = null
        if (iframeWrapId == 'parentNode') {
            iframeWrapObj = document.getElementById(iframeWrapId);

        } else if (iframeId) {
            iframeWrapObj = document.getElementById(iframeId).parentNode;
        }
        let iframeObj = iframeWrapObj ? iframeWrapObj.children[0] : {};
        iframeObj.src = approveUrl;
        if (iframeWrapObj) {
            iframeWrapObj.innerHTML = iframeWrapObj.innerHTML;
        }

        //iframe里的单据页面宽度自适应，去掉边距
        let newIframeObj = iframeWrapObj.children[0]
        newIframeObj.onload = () => {
            if (newIframeObj.contentWindow) {
                

                let sWin = newIframeObj.contentWindow;
                let sDoc = sWin.document;
                sDoc.body.style.padding = 0;
                sDoc.body.style.minWidth = 'auto';
                sWin.app.style.minWidth = 'auto';
                let style = sDoc.createElement("style");
                sDoc.head.appendChild(style);
                style.sheet.insertRule(".card-table-modal-dialog.slideInRight  { width:95%}", 0);
                style.sheet.insertRule(".maxbox {padding: 0 }", 0);

            }
        }
    }

    return approveUrl;
}

export default urlHandle;